<?php
    class HtmlString
    {
        public function getDate($result, $message, $type)
        {
            $SearchString = $message;
            $FirstChr = strpos($result,$SearchString);

            try
            {
                $FirstChr1 = length($result) - 1;
                $yo = $FirstChr1 - $FirstChr; // *********
                $strTemp = $result.Substring(FirstChr, $yo);   // *********
                if ($type == "kar")
                {
                    $url = $this->cleanUrl($strTemp, "<div>", "</div>", "kar");
                    $result = url.Remove(0, 5);
                }
                else if (type == "sky")
                {
                    url = cleanUrl(strTemp, "advertValues" + "\"" + ">", "</span>", "sky");
                    result = url.Remove(0, 9);
                }
            }
            catch (Exception $ex)
            {
                //result = "";
                result = Convert.ToString(System.DateTime.Now);
                result = result.Substring(0, 10);
                result.Trim();
                string[] words = result.Split('/');
                result = words[1] + "/" + words[0] + "/" + words[2];
            }
            return result;
        }

        /**
         * Cleans htmlcode
         * @param url
         *  the html code to be cleaned
         * @param first
         *  from..
         * @param second
         *   to..
         * @return result_
         *   The html code cleaned
         */
        public string cleanUrl(string url, string first, string second, string type)
        {
            string strTemp = "";
            try
            {
                string SearchString = first;
                int FirstChr = url.IndexOf(SearchString);
                string SearchString1 = second;
                int FirstChr1 = url.IndexOf(SearchString1);
                int yo = FirstChr1 - FirstChr;
                strTemp = url.Substring(FirstChr, yo);
                if (type == "sky")
                {
                    strTemp = strTemp.Remove(0, 6);
                }
            }
            catch (Exception ex)
            {
                strTemp = "";
            }
            return strTemp;
        }

        /**
         * Check if page exists
         * @param url
         *   html page
         * @return result_
         *   True if exists False if page doesn't exist
         */
        public bool pageExists(string url, string message)
        {
            int FirstChr = url.IndexOf(message);
            if (FirstChr == -1)
                return true;
            else
                return false;
        }

        /**
* Cleans source code from html tags
* @param source
*   source code to be cleaned
* @return
*   clened source code
*/
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }
    }
}
?>
